package tags;

import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

public class GetTag extends TagSupport {

    private String name;
    private String role;
    private final static Logger logs = Logger.getLogger(InsertTag.class);


    public int doStartTag() throws JspException {
        Stack stack = (Stack)pageContext.getAttribute("stack", PageContext.REQUEST_SCOPE);
        if (stack==null) {
            throw new JspException("No stack");
        }
        HashMap hashMap = (HashMap)stack.peek();
        if (hashMap==null) {
            throw new JspException("No HashMap");
        }
        String parameter = (String)hashMap.get(this.name);
        try {
            pageContext.include(parameter);
        } catch (ServletException | IOException e) {
            throw new JspException("Error in page's name with parameter " + this.name);
        }
        return SKIP_BODY;
    }

    public void release() {
        name = null;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}
